package com.jsbj.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.base.ResultBean;
import com.jsbj.tools.RequestHeaderUtils;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysRoleUser;
import com.jsbj.user.mapper.SysRoleUserMapper;
import com.jsbj.user.service.ISysRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 角色-人员信息表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-03-07
 */
@Service
public class SysRoleUserServiceImpl extends ServiceImpl<SysRoleUserMapper, SysRoleUser> implements ISysRoleUserService {

    @Autowired
    private HttpServletRequest request;

    @Override
    public ResultBean<Integer> insert(String inParam) {
        String userId = RequestHeaderUtils.getUserId(request);
        JSONObject object = JSONObject.parseObject(inParam);
        String userIds = object.getString("userIds");
        String roleId = object.getString("roleId");
        if (StringUtils.isEmpty(userIds) || StringUtils.isEmpty(roleId)) {
            return new ResultBean<>().build("必填项不能为空！");
        }

        // 先删除
        List<String> userIdList = Arrays.asList(userIds.split(","));
        LambdaUpdateWrapper<SysRoleUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysRoleUser::getDisabled, 0);
        updateWrapper.eq(SysRoleUser::getRoleId, roleId);
        updateWrapper.notInSql(SysRoleUser::getUserId, "select id from sys_user where disabled = 0 and is_status = 0");
        updateWrapper.notIn(SysRoleUser::getUserId, userIdList);
        SysRoleUser sysRoleUser1 = new SysRoleUser();
        sysRoleUser1.setDisabled(1);
        sysRoleUser1.setUpdateId(userId);
        sysRoleUser1.setUpdateTime(new Date());
        baseMapper.update(sysRoleUser1, updateWrapper);

        for (String uId : userIdList) {
            LambdaQueryWrapper<SysRoleUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysRoleUser::getDisabled, 0);
            queryWrapper.eq(SysRoleUser::getRoleId, roleId);
            queryWrapper.eq(SysRoleUser::getUserId, uId);
            queryWrapper.last(" limit 1");
            // 删除没有的，新增新增的
            if (null == baseMapper.selectOne(queryWrapper)) {
                SysRoleUser sysRoleUser = new SysRoleUser();
                sysRoleUser.setUserId(uId);
                sysRoleUser.setRoleId(roleId);
                sysRoleUser.setCreaterId(userId);
                sysRoleUser.setCreateTime(new Date());
                sysRoleUser.setUpdateId(userId);
                sysRoleUser.setUpdateTime(new Date());
                baseMapper.insert(sysRoleUser);
            }

        }
//        String ids = object.getString("ids");
//        if (StringUtils.isNotEmpty(ids)) {
//            List<String> notChooseIds = Arrays.asList(ids.split(","));
//            for (String id : notChooseIds) {
//                SysRoleUser sysRoleUser = baseMapper.selectById(id);
//                sysRoleUser.setDisabled(1);
//                sysRoleUser.setUpdateId(userId);
//                sysRoleUser.setUpdateTime(new Date());
//                baseMapper.updateById(sysRoleUser);
//            }
//        }
        return new ResultBean<>().success("处理成功！");
    }

    @Override
    public List<Map<String, Object>> detail(String roleId) {
        List<SysRoleUser> sysUsers = baseMapper.selectListAll(roleId);
        List<Map<String, Object>> retList = new ArrayList<>();
        for (SysRoleUser sysRoleUser : sysUsers) {
            Map<String, Object> ret = new HashMap<>();
            ret.put("userId", sysRoleUser.getUserId());
            ret.put("userName", sysRoleUser.getUserName());
            retList.add(ret);
        }
        return retList;
    }

    @Override
    public void insertData(String tenantSuperAccount, String roleId, String userId) {
        LambdaQueryWrapper<SysRoleUser> qw = new LambdaQueryWrapper<>();
        qw.eq(SysRoleUser::getDisabled, 0);
        qw.eq(SysRoleUser::getUserId, tenantSuperAccount);
        qw.eq(SysRoleUser::getRoleId, roleId);
        List<SysRoleUser> sysUsers = baseMapper.selectList(qw);
        if (CollectionUtils.isEmpty(sysUsers)) {
            SysRoleUser sysRoleUser = new SysRoleUser();
            sysRoleUser.setUserId(tenantSuperAccount);
            sysRoleUser.setRoleId(roleId);
            sysRoleUser.setCreaterId(userId);
            sysRoleUser.setCreateTime(new Date());
            sysRoleUser.setUpdateId(userId);
            sysRoleUser.setUpdateTime(new Date());
            baseMapper.insert(sysRoleUser);
        }
    }

    @Override
    public void deleteData(String userId, String roleId, String sys) {
        LambdaUpdateWrapper<SysRoleUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysRoleUser::getDisabled, 0);
        updateWrapper.eq(SysRoleUser::getUserId, userId);
        updateWrapper.eq(SysRoleUser::getRoleId, roleId);

        SysRoleUser sysRoleUser = new SysRoleUser();
        sysRoleUser.setDisabled(1);
        sysRoleUser.setUpdateTime(new Date());
        sysRoleUser.setUpdateId(userId);
        baseMapper.update(sysRoleUser, updateWrapper);
    }
}
